# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2009  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys
sys.path.append('../')

from PyQt4.QtGui import *
from PyQt4 import QtCore

import editor

class CompoundVisual(QGraphicsItem):

	font = QFont()
	paddingVertical = 2
	paddingHorizontal = 2
	_fontMetrics = QFontMetrics(font)
	height = _fontMetrics.height()+2*paddingVertical

	# Milliseconds to wait until we show the compound structure
	hoverTime = 200

	def __init__(self, compound, parent=None):
		QGraphicsItem.__init__(self, parent)
		
		self.compound = compound
		self.name = compound.name

		self.rect = QtCore.QRectF()

		self.width = self._fontMetrics.width(self.name)+self.paddingHorizontal

		# Timer to see if the mouse has been still long enough to display structure
		self.timer = QtCore.QTimer()
		QtCore.QObject.connect(self.timer, QtCore.SIGNAL("timeout()"), self.showCompoundStructure)

		# Accept hover events to pop up structure
		self.setAcceptHoverEvents(True)

		self.showingStructure = False
		self.hoverPos = QtCore.QPointF(0,0)
		self.structure = None

		self.calcRect()

		self.setZValue(0)

	def setMaxWidth(self, width):
		if self.width <= width:
			return

		i = len(self.name)-1
		while self._fontMetrics.width(self.name[:i]+'...')+self.paddingHorizontal > width:
			i -= 1

		self.name = self.name[:i]+'...'
		self.width = self._fontMetrics.width(self.name)+self.paddingHorizontal
		self.calcRect()

	def calcRect(self):
		self.rect = QtCore.QRectF(-self.width/2, -self.height/2, self.width, self.height)
		self.prepareGeometryChange()

	def boundingRect(self):
		return self.rect

	def paint(self, painter, option, widget):
		#painter.drawRect(self.boundingRect())
		
		painter.drawText(-self.width/2+self.paddingHorizontal, self.height/2-self.paddingVertical, self.name)

	def hoverMoveEvent(self, event):
		self.timer.stop()
		if self.showingStructure:
			self.scene().removeItem(self.structure)
			self.structure = None
			self.showingStructure = False
		
		self.timer.start(self.hoverTime)
		self.hoverPos = event.scenePos()

	def hoverLeaveEvent(self, event):
		self.timer.stop()
		if self.showingStructure:
			self.scene().removeItem(self.structure)
			self.structure = None
			self.showingStructure = False

	def mousePressEvent(self, event):
		self.timer.stop()
		if self.showingStructure:
			self.scene().removeItem(self.structure)
			self.structure = None
			self.showingStructure = False
		QGraphicsItem.mousePressEvent(self, event)

	def showCompoundStructure(self):
		self.timer.stop()
		self.showingStructure = True
		self.structure = CompoundPopup(self.compound)
		self.scene().addItem(self.structure)
		self.structure.setPos(self.hoverPos+self.structure.boundingRect().bottomRight())


class CompoundPopup(QGraphicsItem):

	padding = 10

	def __init__(self, obj, parent=None):
		QGraphicsItem.__init__(self, parent)

		self.rect2 = QtCore.QRectF()

		self.compound = editor.Compound(obj.molfile, parent=self)

		self.compound.setPos(0,0)

		self.setZValue(100)

	def boundingRect(self):
		return self.childrenBoundingRect().adjusted(-self.padding, -self.padding, self.padding*2, self.padding*2)

	def paint(self, painter, option, widget):
		pen = QPen(QColor(167,204,241))
		pen.setJoinStyle(QtCore.Qt.RoundJoin)
		pen.setWidth(4)
		painter.setPen(pen)
		painter.drawRect(self.boundingRect().adjusted(2,2,-4,-4))
		painter.fillRect(self.boundingRect().adjusted(2,2,-4,-4), QBrush(QColor(255,255,255,230)))

	